package com.example.digitaldiary;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {

	public static final String ID_EXTRA = "DigitalDiary_ID_Extra";
	private DataHelper helper;
	private Cursor cursor;

	private ListView listView;
	private LazyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		helper = new DataHelper(this);
		cursor = helper.getAll();
		startManagingCursor(cursor);
		adapter = new LazyAdapter(this, cursor, helper);
		listView = (ListView) findViewById(R.id.list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// start Editor for item that was clicked
				Intent intent = new Intent(MainActivity.this, EditorActivity.class);
				intent.putExtra(ID_EXTRA, String.valueOf(arg3));
				startActivity(intent);
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// Show Dialog for View, Edit, Delete options
//				final long id = arg3;
				LongClickDialog dialog = new LongClickDialog(arg3);
				dialog.show(getFragmentManager(), null);
				Toast.makeText(getBaseContext(), "Long clicked", Toast.LENGTH_SHORT);
				return true;
			}
		});
	}
class LongClickDialog extends DialogFragment
{
	private long id;
	public LongClickDialog(long id)
	{
		this.id = id;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setTitle("What do you want?")
			   .setItems(new String[] {"View Entry", "Edit Entry", "Delete Entry"}, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch(which)
					{
					case 0:
						break;
					case 1:
						Intent intent = new Intent(MainActivity.this, EditorActivity.class);
						intent.putExtra(ID_EXTRA, String.valueOf(id));
						startActivity(intent);
						break;
					case 2:
						helper.deleteById(String.valueOf(id));
						cursor.requery();
					}
				}
			});
		return builder.create();
	}
}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		helper.close();
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_entry:
			startActivity(new Intent(this, EditorActivity.class));
			break;
		case R.id.main_map:
			// Display map with marked locations of all entries
			break;
		case R.id.menu_settings:
			// Show dialog for settings
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

	}

}
